package com.financing.controller.user;
import com.financing.components.payment.Gateway;
import com.financing.model.Data;
import com.financing.util.CheckIdCard;
import com.financing.util.DateUtil;
import com.financing.util.Json;
import com.financing.util.Str;
import com.financing.util.XmlHelper;
import com.jfinal.core.Controller;

/**
 *  企业用户控制器
 * @since 2017-06-06
 * @author 无处不在的二叔
 */
public class FundController extends Controller {

	/**
	 * 企业开户
	 */
	public void index() throws Exception {		
		// http://www.wlcfull.com/fund?token=4eb4948df5c8bab15c78182f2708973a&certNum=330225198911140356&realName=杜恒翔&orgName=浙江睿弘资产管理有限公司&mobile=13586579333&bank=0302&city=3320&account=8114701013600253987&email=440759591@qq.com
		String certNum = getPara("certNum");
		String realName = getPara("realName");
		String orgName = getPara("orgName");
		CheckIdCard card = new CheckIdCard(certNum);

		// 内容检查
		if(Str.nb(certNum) && Str.nb(realName) ) {
			// 检查身份证格式
			if(card.validate()){
				XmlHelper res = Gateway.post("/artifReg.action", 
					Data.put(
						"mchnt_txn_ssn",DateUtil.getTimeStamp(), 
						"cust_nm",orgName, 
						"artif_nm",realName,
						"certif_id",certNum,
						"mobile_no",getPara("mobile"),
						"email",getPara("email"),
						"city_id",getPara("city"),
						"parent_bank_id",getPara("bank"),
						"bank_nm","",
						"capAcntNo",getPara("account"),
						"password","",
						"lpassword","",
						"rem","融资机构"+orgName+"开户",
						"sign", "artif_nm,bank_nm,capAcntNo,certif_id,city_id,cust_nm,email,lpassword,mchnt_cd,mchnt_txn_ssn,mobile_no,parent_bank_id,password,rem,ver"
					)
				);
				
				if(res.isSuccess()){
					
					/**User fund = new User();
					fund.setCode(getPara("mobile"))
						.setRealName(orgName)
						.setMobile(getPara("mobile"))
						.setEmail(getPara("email"))
						.setCertNum(certNum)	//身份证号码
						.setBirthday(card.getBirthday())	// 第7~14位数字表示：出生年、月、日
						.setSex(card.isMale()?1:2) // 第17位数字表示性别：奇数表示男性，偶数表示女性； 
						.setIsBindBankCard(1)
						.setBankCard(getPara("account"))
						.setBindCardTime(new Date())
						.save();*/
					
					renderJson(Json.ok("用户实名成功！"));
				}else{
					renderJson(Json.error(res.getErrorMsg()));
				} 
			} else {
				renderJson(Json.error("身份证格式不正确！"));
			} 
		} else {
			renderJson(Json.error("数据不可为空！"));
		} 
	}
}
 